package com.yzw.phonestoreservice.config;

import com.yzw.phonestoreservice.interceptor.MyInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: springbootdome
 * @BelongsPackage: com.yu.sbmpsum1.config
 * @Author: yanhongwei
 * @CreateTime: 2022-10-30  17:06
 * @Description: TODO 自定义拦截器
 * @Version: 1.0
 */
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
    private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 实现WebMvcConfigurer不会导致静态资源被拦截,单体项目中
        registry.addInterceptor(new MyInterceptor())
                // 拦截所有url
                .addPathPatterns("/**")
                // 放行swagger
                .excludePathPatterns("/swagger-resources/**");
    }

    @Override //解决跨域问题
    public void addCorsMappings(CorsRegistry registry) {
        logger.info("解决跨域问题,携带各种头");
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","POST","PUT","DELETE","HEAD","OPTIONS")
                .allowCredentials(true)
                .allowedHeaders("*");
    }
}